<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { getProductDetail } from "@/api/product";
import { formatDate } from "@/utils/format";
import { ElMessage } from "element-plus";
import { addToCart as addToCartApi } from "@/api/cart";
import { Picture, ShoppingCart } from "@element-plus/icons-vue";

interface Product {
  id: number;
  name: string;
  price: number;
  description: string;
  image: string;
  sales: number;
  expirationDate: string;
  type: number;
  avatar: string;
}

const route = useRoute();
const router = useRouter();
const loading = ref(false);
const product = ref<Product | null>(null);
const quantity = ref(1);

// 获取商品详情
const getDetail = async () => {
  loading.value = true;
  try {
    const productId = Number(route.params.id);
    if (isNaN(productId)) {
      ElMessage.error("无效的商品ID");
      router.push("/tourist/products");
      return;
    }

    const res = await getProductDetail(productId);
    if (res.success) {
      product.value = res.data;
    }
  } catch (error) {
    console.error("获取商品详情失败：", error);
    ElMessage.error("获取商品详情失败");
  } finally {
    loading.value = false;
  }
};

// 格式化日期
const formatDateTime = (time: string) => {
  if (!time) return "";
  return formatDate(new Date(time), "YYYY-MM-DD");
};

// 添加到购物车
const handleAddToCart = async () => {
  if (!product.value) {
    ElMessage.error("商品信息不存在");
    return;
  }
  try {
    const res = await addToCartApi(product.value.id, quantity.value);
    if (res.success) {
      ElMessage.success("添加成功");
    } else {
      ElMessage.error(res.message || "添加失败");
    }
  } catch (error) {
    console.error("添加失败:", error);
    ElMessage.error("添加失败");
  }
};

onMounted(() => {
  getDetail();
});
</script>

<template>
  <div class="product-detail-container">
    <div v-if="loading" class="loading-container">
      <el-skeleton :rows="10" animated />
    </div>

    <div v-else-if="!product" class="error-container">
      <el-empty description="商品不存在" />
      <el-button type="primary" @click="router.push('/tourist/products')">
        返回商品列表
      </el-button>
    </div>

    <div v-else class="product-detail-card">
      <div class="product-image">
        <el-image
          :src="product.avatar"
          fit="cover"
          :preview-src-list="[product.avatar]"
          :initial-index="0"
        >
          <template #placeholder>
            <div class="image-slot">
              <el-icon class="is-loading"><Picture /></el-icon>
            </div>
          </template>
          <template #error>
            <div class="image-slot">
              <el-icon><Picture /></el-icon>
              <span>加载失败</span>
            </div>
          </template>
        </el-image>
      </div>
      <div class="product-info">
        <h1 class="product-title">{{ product.name }}</h1>
        <div class="product-desc">{{ product.description }}</div>

        <div class="price-sales-info">
          <div class="price">{{ product.price }}</div>
          <div class="sales">销量：{{ product.sales }}</div>
        </div>

        <div class="product-meta">
          <div class="meta-item">
            <span class="label">有效期至：</span>
            <span class="value">{{
              formatDateTime(product.expirationDate)
            }}</span>
          </div>
        </div>

        <div class="quantity-section">
          <div class="quantity-control">
            <el-input-number
              v-model="quantity"
              :min="1"
              :max="99"
              size="large"
              :controls-position="'right'"
            />
          </div>
          <el-button
            type="primary"
            size="large"
            class="add-to-cart-btn"
            @click="handleAddToCart"
          >
            <el-icon><ShoppingCart /></el-icon>
            加入购物车
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.product-detail-container {
  padding: 32px;
  min-height: calc(100vh - 60px);
  background: #f8fafc;
  display: flex;
  justify-content: center;
}

.product-detail-card {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
  padding: 32px;
  display: grid;
  grid-template-columns: 500px 1fr;
  gap: 40px;
  width: 100%;
  max-width: 1200px;
}

.product-image {
  width: 100%;
  height: 500px;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);

  .el-image {
    width: 100%;
    height: 100%;
    transition: transform 0.3s ease;

    &:hover {
      transform: scale(1.02);
    }
  }
}

.product-info {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.product-title {
  font-size: 28px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0;
}

.product-desc {
  font-size: 16px;
  color: #666;
  line-height: 1.6;
}

.price-sales-info {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 16px 0;
  border-top: 1px solid #f0f0f0;
  border-bottom: 1px solid #f0f0f0;
}

.price {
  color: #ff4d4f;
  font-size: 32px;
  font-weight: 600;
  position: relative;
  padding-left: 20px;

  &::before {
    content: "¥";
    font-size: 20px;
    position: absolute;
    left: 0;
    top: 4px;
  }
}

.sales {
  color: #999;
  font-size: 14px;
  background: #f5f7fa;
  padding: 6px 16px;
  border-radius: 20px;
}

.product-meta {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 8px;

  .label {
    color: #666;
    font-size: 14px;
    min-width: 80px;
  }

  .value {
    color: #1a1a1a;
    font-size: 14px;
  }
}

.quantity-section {
  display: flex;
  align-items: center;
  gap: 24px;
  margin-top: 16px;
}

.quantity-control {
  :deep(.el-input-number) {
    width: 160px;

    .el-input__wrapper {
      padding-right: 0;
    }

    .el-input-number__decrease,
    .el-input-number__increase {
      background: #f5f7fa;
      border: none;
      color: #666;

      &:hover {
        color: #409eff;
      }
    }
  }
}

.add-to-cart-btn {
  flex: 1;
  height: 48px;
  font-size: 16px;
  border-radius: 24px;
  background: linear-gradient(135deg, #409eff, #3a8ee6);
  border: none;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
  }

  .el-icon {
    margin-right: 8px;
    font-size: 18px;
  }
}

.image-slot {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  font-size: 24px;
}

@media screen and (max-width: 1200px) {
  .product-detail-container {
    padding: 24px;
  }

  .product-detail-card {
    padding: 24px;
    gap: 32px;
  }
}

@media screen and (max-width: 992px) {
  .product-detail-card {
    grid-template-columns: 1fr;
  }

  .product-image {
    height: 400px;
  }
}

@media screen and (max-width: 768px) {
  .product-detail-container {
    padding: 16px;
  }

  .product-detail-card {
    padding: 16px;
    gap: 24px;
  }

  .product-image {
    height: 300px;
  }

  .product-title {
    font-size: 24px;
  }

  .price {
    font-size: 28px;
  }

  .quantity-section {
    flex-direction: column;
    gap: 16px;
  }

  .quantity-control {
    width: 100%;

    :deep(.el-input-number) {
      width: 100%;
    }
  }

  .add-to-cart-btn {
    width: 100%;
  }
}

.loading-container,
.error-container {
  width: 100%;
  max-width: 1200px;
  background: #fff;
  border-radius: 16px;
  padding: 32px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
}

.error-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
}
</style> 